package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.service.RoleService;
import com.itheima.domain.Role;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    /*public void setUserService(UserService userService){
        this.userService=userService;
    }*/

    @RequestMapping(value = "/login")
    public String login(String username,String password,HttpSession session){
        System.out.println("userController.login...");
        System.out.println(userService);
        User user = userService.login(username,password);
        if(user!=null){
            session.setAttribute("user",user);
            return "redirect:/index.jsp";
        }
        return "redirect:/login.jsp";
    }

    @RequestMapping("/saveUI")
    public String saveUI(Model model){
        List<Role> roleList = roleService.roleList();
        model.addAttribute("roleList",roleList);
        return "user-add";
    }

    @RequestMapping("/userAdd")
    public String userSave(User user,Long[] roleIds){
        userService.userSave(user,roleIds);
        return "redirect:/user/userList";
    }

    @RequestMapping("/delete/{userId}")
    public String delete(@PathVariable("userId") Long userId){
        userService.delete(userId);
        System.out.println("=========================");
        return "redirect:/user/userList";
    }



    @RequestMapping("/userList")
    public String userList(Model model){
        List<User> userList = userService.userList();
        model.addAttribute("userList",userList);
        return "user-list";
    }

    @RequestMapping(value = "/save",method = RequestMethod.GET,params = {"username"})
    public String save(){
        System.out.println("userController.save...");
        return "success";
    }

    @RequestMapping("/save2")
    public ModelAndView save2(){
        System.out.println("userController.save2...");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","itcast");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/save3")
    public ModelAndView save3(ModelAndView modelAndView){
        System.out.println("userController.save3...");
        modelAndView.addObject("username","itheima");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/save4")
    public String save4(Model model){
        System.out.println("userController.save4...");
        model.addAttribute("username","hello");
        return "success";
    }

    @RequestMapping("/save5")
    public String save5(HttpServletRequest request){
        System.out.println("userController.save5...");
        request.setAttribute("username","request");
        return "success";
    }

    @RequestMapping("/save6")
    public void save6(HttpServletResponse response) throws IOException {
        System.out.println("userController.save6...");
        response.getWriter().print("response");
    }

    @RequestMapping("/save7")
    @ResponseBody
    public String save7() {
        System.out.println("userController.save7...");
        return "hhhhh";
    }

    @RequestMapping("/save8")
    @ResponseBody
    public String save8() {
        System.out.println("userController.save8...");
        return "{\"username\":\"zs\",\"age\":20}";
    }

    @RequestMapping("/save9")
    @ResponseBody
    public String save9() throws IOException {
        System.out.println("userController.save9...");
        User user = new User(null,"zs","123","gg@qq.com","12345678901",null);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
    }

    @RequestMapping("/save10")
    @ResponseBody
    public User save10() {
        System.out.println("userController.save10...");
        User user = new User(null,"zs","123","gg@qq.com","12345678901",null);
        return user;
    }

    @RequestMapping("/save11")
    @ResponseBody
    public void save11(String username,String password) {
        System.out.println("userController.save11...");
        System.out.println(username);
        System.out.println(password);
    }

    @RequestMapping("/save12")
    @ResponseBody
    public void save12(User user) {
        System.out.println("userController.save12...");
        System.out.println(user);
    }

    @RequestMapping("/save13")
    @ResponseBody
    public void save13(String[] s) {
        System.out.println("userController.save13...");
        System.out.println(Arrays.asList(s));
    }

    /*@RequestMapping("/save14")
    @ResponseBody
    public void save14(VO vo) {
        System.out.println("userController.save14...");
        System.out.println(vo);
    }*/

    @RequestMapping("/save15")
    @ResponseBody
    public void save15(@RequestBody List<User> userList) {
        System.out.println("userController.save15...");
        System.out.println(userList);
    }

    @RequestMapping("/save16")
    @ResponseBody
    public void save16(@RequestParam(value = "name",required = false) String username,@RequestParam(value = "password",defaultValue = "111") String password) {
        System.out.println("userController.save16...");
        System.out.println(username);
        System.out.println(password);
    }

    @RequestMapping(value = "/save17/{name}&{password}",method = RequestMethod.GET)
    @ResponseBody
    public void save17(@PathVariable(value = "name",required = false) String username,@PathVariable(value = "password",required = false) String password) {
        System.out.println("userController.save17...");
        System.out.println(username);
        System.out.println(password);
    }

    @RequestMapping(value = "/save18")
    @ResponseBody
    public void save18(Date date) {
        System.out.println("userController.save18...");
        System.out.println(date);
    }

    @RequestMapping(value = "/save19")
    @ResponseBody
    public void save19(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        System.out.println("userController.save19...");
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }

    @RequestMapping(value = "/save20")
    @ResponseBody
    public void save20(@RequestHeader(value = "User-Agent",required = false) String user_agent) {
        System.out.println("userController.save20...");
        System.out.println(user_agent);
    }

    @RequestMapping(value = "/save21")
    @ResponseBody
    public void save21(@CookieValue(value = "JSESSIONID",required = false) String jsessionId) {
        System.out.println("userController.save21...");
        System.out.println(jsessionId);
    }

    @RequestMapping(value = "/save22")
    @ResponseBody
    public void save22(String username, MultipartFile uploadFile) throws IOException {
        System.out.println("userController.save22...");
        System.out.println(username);
        System.out.println(uploadFile);

        //获取文件名
        String originalFilename = uploadFile.getOriginalFilename();

        //保存文件
        uploadFile.transferTo(new File("E:\\test\\"+originalFilename));
    }

    @RequestMapping(value = "/save23")
    @ResponseBody
    public void save23(String username, MultipartFile uploadFile1,MultipartFile uploadFile2) throws IOException {
        System.out.println("userController.save23...");
        System.out.println(username);
        System.out.println(uploadFile1);

        //获取文件名
        String originalFilename1 = uploadFile1.getOriginalFilename();
        String originalFilename2 = uploadFile2.getOriginalFilename();

        //保存文件
        uploadFile1.transferTo(new File("E:\\test\\"+originalFilename1));
        uploadFile2.transferTo(new File("E:\\test\\"+originalFilename2));
    }

    @RequestMapping(value = "/save24")
    @ResponseBody
    public void save24(String username, MultipartFile[] uploadFile) throws IOException {
        System.out.println("userController.save24...");
        System.out.println(username);
        System.out.println(uploadFile);

        for(MultipartFile u:uploadFile){
            String originalFilename = u.getOriginalFilename();
            u.transferTo(new File("E:\\test\\"+originalFilename));
        }

        //获取文件名
        //String originalFilename1 = uploadFile1.getOriginalFilename();
        //String originalFilename2 = uploadFile2.getOriginalFilename();

        //保存文件
        //uploadFile1.transferTo(new File("E:\\test\\"+originalFilename1));
        //uploadFile2.transferTo(new File("E:\\test\\"+originalFilename2));
    }

}
